package com.ysd.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.entity.Modules;
import com.ysd.entity.Roles;
import com.ysd.service.ModulesService;
import com.ysd.service.RolesService;
import com.ysd.util.ResultInfoUtil;
import com.ysd.util.Shuttle;
import com.ysd.util.Treemodule;
import com.ysd.util.Treenode;

@RestController
@RequestMapping("/roles")
@CrossOrigin
public class RolesController {
	@Autowired
	private RolesService rolesService;
	@Autowired
	private ModulesService modulesService;

	@GetMapping
	public ResultInfoUtil findAllRoles(Integer limit, Integer page, Roles r) {
		System.out.println("分页查询角色");
		Pageable pageable = PageRequest.of(page - 1, limit);
		Page<Roles> rol = rolesService.findAllRoles(r, pageable);
		return new ResultInfoUtil().success(rol.getContent(), rol.getTotalElements());
	}

	/**
	 * 角色删除
	 * 
	 * @param rid
	 * @return
	 */
	@DeleteMapping
	public Integer deleteRoles(Roles r) {

		try {
			rolesService.deleteRoles(r);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			return 2;
		}
	}

	/**
	 * 添加角色
	 * 
	 * @param r
	 * @return
	 */
	@PostMapping("/add")
	public Roles addRoles(Roles r) {
		String id = String.valueOf(UUID.randomUUID());
		r.setId(id);
		return rolesService.addAndUpdate(r);
	}

	/**
	 * 角色修改
	 * 
	 * @param r
	 * @return
	 */
	@PutMapping("/update")
	public Roles updateRoles(Roles r) {

		return rolesService.addAndUpdate(r);
	}

	// -------------------------------------------------
	/**
	 * 查询出来所有的角色
	 */
	@GetMapping("/getall")
	public List<Shuttle> findallroles() {
		List<Roles> r = rolesService.findAllRoles();
		List<Shuttle> ru = new ArrayList<Shuttle>();
		for (int i = 0; i < r.size(); i++) {
			ru.add(new Shuttle(r.get(i).getId(), r.get(i).getName()));
		}
		return ru;

	}

	@GetMapping("/getall1")
	public List<Roles> finda() {
		List<Roles> r = rolesService.findAllRoles();
		return r;
	}

	/*	*//**
			 * 根据角色id得到所属模块
			 * 
			 * @param roleid
			 * @return
			 *//*
				 * @GetMapping("/ByidfindModules") public List<Treenode> ByidfindModules(String
				 * Id){ //List<Modules> System.out.println("根据角色id得到所属模块"+Id); Optional<Roles>
				 * r=rolesService.ByidfindModules(Id); List<Modules> mlist=r.get().getModules();
				 * System.out.println(r); List<Treenode>
				 * newmodules=modulesService.getTreefront(mlist); return newmodules;
				 * 
				 * }
				 */

	// 获取该用户的功能菜单
	@RequestMapping("/getFunctionMenu")
	public List<Modules> getFunctionMenu(HttpServletRequest request, String roleids) {

		String[] result = roleids.split(",");

		System.out.println(roleids);

		// 将一维数组转换为list
		List<String> rids = new ArrayList<String>(Arrays.asList(result));
		// 根据用户角色获取所有功能模块
		List<Modules> modules = rolesService.functionMenu(rids);

		System.out.println("modules==" + modules.size());
		List<Modules> newmodules = modulesService.getTree(modules);
		System.out.println(1 + "newmodules" + newmodules);
		// System.out.println("newmodules=="+newmodules.get(0).getChilder().get(0).getName());
		System.out.println(2);
		// 转换为树结构
		return newmodules;
	}

	/**
	 * 根据角色id得到所属模块 ：角色模块
	 * 
	 * @param roleid
	 * @return
	 */
	@GetMapping("/ByidfindModules")
	public  List<Treemodule> ByidfindModules(String Id){
		//List<Modules>
		System.out.println("roleid=="+Id);
	  Optional<Roles> r=rolesService.ByidfindModules(Id);
	  System.out.println("r"+r.get());
	  List<Modules>	mlist=r.get().getModules();
	  System.out.println(mlist.size());
	 // getModulesTree
	 
	 // getModulesTree
	 // List<Treemodule> newmodules=modulesService.getTreefront(mlist);
	  List<Treemodule> newmodules=modulesService.getModulesTree(mlist);
	  System.out.println("newmodules"+newmodules.size());
	  
	  List<Treemodule>  t=new ArrayList<Treemodule>();
	  
	  for (int i = 0; i < newmodules.size(); i++) {
		  System.out.println(newmodules.get(i).getChildren().size());
		  //System.out.println(newmodules.get(i).getChildren());
		  if(newmodules.get(i).getChildren().size()>0)
			 for (int j = 0; j < newmodules.get(i).getChildren().size(); j++) {
				 t.add(newmodules.get(i).getChildren().get(j));
			}
			 
		  }
	  return t;
	}
	
	

}
